exports.handler = function(sid, query, callback) {
    var db = gServers.getGameDB(sid);
    if (!db) {
       callback(util.format('sid=%d db eror', sid)); return;
    }
    
    var minLevel = query.level || 1;
    var dbWorld = db.collection('world');
    var leagueDoc = null;
    var leagueDuplicateDoc = null;

    async.series([
        function(cb) {
            dbWorld.findOne({_id: 'league'}, {}, function(err, doc) {
                leagueDoc = doc;
                cb(err);
            });
        },
        function(cb) {
            dbWorld.findOne({_id: 'league_duplicate'}, {}, function(err, doc) {
                leagueDuplicateDoc = doc;
                cb(err);
            });
        },
        function(cb) {
            var result = {};
            objectForEach(leagueDoc.league, function(name, league) {
                addAttrValue(result, "all", 1);
                if (league.level <= minLevel) {
                    addAttrValue(result, "del", 1);
                    delete leagueDuplicateDoc.leagues[league.duplicate];
                    delete leagueDoc.league[name];
                }
            });
            LOG(util.format('sid=%d result=%j', sid, result));
            cb();
        },
        function(cb) {
            dbWorld.save(leagueDoc, function(err, result) {
                cb(err);
            });
        },
        function(cb) {
            dbWorld.save(leagueDuplicateDoc, function(err, result) {
                cb(err);
            });
        }
    ], function(err, results) {
        callback(err);
    });
}

exports.help = function() {
    return [
        "[level=1]",
        "清理军团,军团等级<=level",
    ];
}

exports.master = true;
